package io.gbznt.config;

import java.util.List;

import io.gbznt.modules.buss.entity.BussSwitchEntity;
import io.gbznt.modules.buss.quartz.DynamicTask;
import io.gbznt.modules.buss.quartz.MyJobswitch;
import io.gbznt.modules.buss.quartz.MyJobweather;
import io.gbznt.modules.buss.service.BussSwitchService;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.gbznt.modules.sys.entity.SysConfigEntity;
import io.gbznt.modules.sys.service.SysConfigService;
 
/**
 * Created by pangkunkun on 2017/9/3.
 */
@Component
@Order(value = 0)
public class MyWeatherStartService implements ApplicationRunner {

	private static Logger logger = LoggerFactory.getLogger(MyWeatherStartService.class);

	@Autowired
	private SysConfigService sysConfigService;
	@Autowired
	private BussSwitchService bussswitch;
	
	
	 public static String JOB_GROUP_NAME = "WEATHER_JOB_GROUP";  
	 public static String TRIGGER_GROUP_NAME = "WEATHER_JOB_GROUP"; 
	
    @Override
    public void run(ApplicationArguments var1) throws Exception{
		logger.info("启动定时存取天气");

        //第二个定时器
        new Thread(new MyRuns()).start();
    }

	/**
	 * 线程执行
	 * 
	 * @author Administrator
	 *
	 */
	class MyRuns implements Runnable {

		public MyRuns() {
			super();
		}

		@Override
		public void run() {
			
			// TODO Auto-generated method stub
			List<SysConfigEntity> slist =  sysConfigService.list(
					  new QueryWrapper<SysConfigEntity>()
					  .eq("param_key","weather")
					  .eq("status",1)
					  
					);
			for (SysConfigEntity sysConfigEntity : slist) {
				if(StringUtils.isNotBlank(sysConfigEntity.getParamValue())){
			    DynamicTask.addJobnot(sysConfigEntity.getId()+"weather", JOB_GROUP_NAME, sysConfigEntity.getId(), TRIGGER_GROUP_NAME, MyJobweather.class, "0 */50 * * * ?",sysConfigEntity.getParamValue());
	
				}
			   
				
			}
			
			//---------------开关读取------------------------
			//DynamicTask.switchjob("sbswitch", JOB_GROUP_NAME, "sbswitch", TRIGGER_GROUP_NAME, MyJobswitch.class, "0/10 * * * * ?",null);
			

		}
	}
    
    
}
